<?
						session_start();	
						extract($_REQUEST); 	extract($_SESSION);
						if(preg_match('/\.\.$/', $path)) $path = dirname(dirname($path));
						if($user) echo "$user @ $server  <a class='sm' href=\"javascript:dot('logout','')\">Logout</a><br>$path<br><br>";
						switch($f) {
									case "login": 
											$_SESSION["user"] = $userl;   $_SESSION["pass"] = $pass;
											$_SESSION["server"] = $server;  exit(header('location:ftp.php'));			
									case "logout":
											session_destroy();	exit(header('location:ftp.php'));
									case "put":
											$fn = $_FILES['upf']['name'].'--'.uniqid();
											move_uploaded_file($_FILES['upf']['tmp_name'], $fn);
											$opts = array('ftp'=>array('overwrite'=> true));
											$cxt = stream_context_create($opts);
											file_put_contents($hostname, readfile($fn), false, $cxt);
											break;
									case "get":
											header('Content-Disposition: attachment; filename='.basename($path));
											ob_clean();  flush();	@readfile("ftp://cdrnet:yeahbuddy@dpsw.info/$path");
											exit;
									case 'del':
											$conn_id = ftp_connect($server);
											$login_result = ftp_login($conn_id, $user, $pass);
											ftp_delete($conn_id,  $path);	ftp_close($conn_id);
											exit(header('location:ftp.php'));
									default:
											if(!$user) break;
											$ls = file_get_contents("ftp://$user:$pass@$server$path/");
											$spl = explode("\n", $ls);  $arr=array();
											foreach($spl as $finfo) {
													$ar = explode(' ',$finfo); $fn = $ar[count($ar)-1];
													if($fn != ".") $arr[] = array($ar[count($ar)-1], $ar[0]);
											}
											foreach($arr as $fn) {
													if($fn[0]=="") continue;
													$f = preg_match('/^d/', $fn[1]) ? "list":"get";
													echo "<a href='javascript:dot(\"$f\", \"$path/$fn[0]\")'>$fn[0]</a> ";
													if($f=="get") echo "<a class='red' href='javascript:dot(\"del\", \"$fn[0]\")'>X</a>";
													echo "<br>";
											}
											break;
						}
?>

<?
		if(!isset($_SESSION["user"]) && $test=="") {
?>
				<form method='POST'>
						Server: <input name='server'><br>
						User: <input name='user'><br>
						Password: <input type='password' name='pass'><br>
						<input type='hidden' name='f' value='login'>
						<input type='submit' value='Login'/>
				</form>			
<?
		} else {
?>
				<form method='POST' enctype='multipart/form-data' action=''>
						<input type='file' name='upf'><input type='hidden' name='f' value='put'>
						<input type=submit value=Upload>
				</form>
				<form method='POST'>
						<input id='f' type='hidden' name='f'>
						<input id='path' type='hidden' name='path'>
				</form>			
<?
}
?>

<style>
  body {font: normal 18px/100% Calibri,Helvetica}
  .sm {font: 12px/100% Arial}
  .red {color:red}
  a {color:black; text-decoration:none}
  a:hover {text-decoration:underline}
</style>

<script>
		function $id(id) {return document.getElementById(id)}
		function dot(f,n) {
				if(f=='del' && !confirm('Delete '+n+'\nAre you sure?')) return;		
				$id('f').value=f; $id('path').value=n; document.forms[1].submit()}
</script>
